#include "GameMenu.h"


GameMenu::GameMenu()
{
}


GameMenu::~GameMenu()
{
}

void GameMenu::Initialize(sf::RenderWindow* window){
	m_renderWindow = window;

	m_clock = new sf::Clock();
	
	NuiInitialize(NUI_INITIALIZE_FLAG_USES_SKELETON);
	// HANDS
	leftHandPosZ = 0.f;
	rightHandPosZ = 0.f;
	leftShoulderPosZ = 0.f;
	rightShoulderPosZ = 0.f;

	centerPosX = 0.f;

	gobackleft = false;
	gobackright = false;
	m_deltaTime = 0.f;
	clockStartR = false;
	timerR = 0.f;
	hitR = false;
	clockStartL = false;
	timerL = 0.f;
	hitL = false;
	missedR = false;
	missedL = false;

	m_backgroundTexture = new sf::Texture;
	m_backgroundTexture->loadFromFile("Assets/Images/Menu/GUI_MainMenu_Background.png");
	m_backgroundSprite = new sf::Sprite;
	m_backgroundSprite->setTexture(*m_backgroundTexture);
	m_backgroundSprite->setPosition(0.f, 0.f);
	
	m_gameSelectTexture	 = new sf::Texture;
	m_gameSelectTexture->loadFromFile("Assets/Images/Menu/GUI_Button_PlayGame_Selected.png");
	m_gameSelectSprite = new sf::Sprite;
	m_gameSelectSprite->setTexture(*m_gameSelectTexture);
	m_gameSelectSprite->setPosition(1230.f, 300.f);

	m_scoreSelectTexture = new sf::Texture;
	m_scoreSelectTexture->loadFromFile("Assets/Images/Menu/GUI_Button_HighScore_Selected.png");
	m_scoreSelectSprite = new sf::Sprite;
	m_scoreSelectSprite->setTexture(*m_scoreSelectTexture);
	m_scoreSelectSprite->setPosition(399.f, 300.f);

	m_selecterTexture = new sf::Texture;
	m_selecterTexture->loadFromFile("Assets/Images/Menu/GUI_MainMenu_SelectionMarker.png");
	m_selecterSprite = new sf::Sprite;
	m_selecterSprite->setTexture(*m_selecterTexture);
	m_selecterSprite->setPosition(930.f, 395.f);

#pragma region Joystick
	joystickConnected = false;
	buttonCount = sf::Joystick::getButtonCount(0);
	hasZ = sf::Joystick::hasAxis(0, sf::Joystick::Z);

	if (sf::Joystick::isConnected(0)) {
		joystickConnected = true;
		if (hasZ == true) {
		} else if (hasZ == false) {
		}
	} else {
		joystickConnected = false;
	}
	joyX = 0;
	joyY = 0;
	joyZ = 0;
#pragma endregion

}

void GameMenu::Update(){
	m_deltaTime = m_clock->restart().asSeconds();

	// KINECT KOD
	NuiSkeletonGetNextFrame(0, &ourFrame); //Get a frame and stuff it into ourframe
	for (int i = 0; i < 6; i++) { //Six times 
		if (ourFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED) {
						float closestDistance = 10000.0f;
			DWORD closestIDs[NUI_SKELETON_MAX_TRACKED_COUNT] = {0, 0};
	for (int i = 0; i < NUI_SKELETON_COUNT; i++) {
      const NUI_SKELETON_DATA & skeleton = ourFrame.SkeletonData[i];
      if (skeleton.eTrackingState != NUI_SKELETON_NOT_TRACKED)
      {
        if (skeleton.Position.z < closestDistance)
        {
          closestIDs[0] = skeleton.dwTrackingID;
          closestDistance = skeleton.Position.z;
        }
      }
 
      if (closestIDs[0] > 0) {
        //m_pNuiSensor->SetTrackedSkeletons(closestIDs); // Track this skeleton
		  NuiSkeletonSetTrackedSkeletons(closestIDs);
      }
	}


			// std::cout << "Right Hand: "; //Print "Right hand:"
			//std::cout << ourFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].z << std::endl; //See more on this line below
			//std::cout << "Left Hand: "; //Print "Left hand:"
			//std::cout << ourFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_LEFT].z << std::endl;
		
			leftHandPosZ = ourFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_LEFT].z;
			rightHandPosZ = ourFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].z;
			leftShoulderPosZ = ourFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_SHOULDER_LEFT].z;
			rightShoulderPosZ = ourFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_SHOULDER_RIGHT].z;

			//-------------Right-----------
			if(rightHandPosZ < rightShoulderPosZ && clockStartR == false){
				clockStartR = true;					
				//std::cout << "right start" << std::endl;
			}

			if(rightHandPosZ){
				timerR += m_deltaTime;
			}

			if(rightHandPosZ < rightShoulderPosZ - 0.35f && timerR < 0.1f && hitR == false){
			//	std::cout << "RIGHT HIT" << std::endl;
				hitR = true;
			}

			if(rightHandPosZ > rightShoulderPosZ - 0.3f && clockStartR == true ){
				//std::cout << "right back" << std::endl;
				timerR = 0.f;
				clockStartR = false;
				hitR = false;
			}
					
			//-------------Left-----------
			if(leftHandPosZ < leftShoulderPosZ && clockStartL == false){
				clockStartL = true;					
				//std::cout << "left start" << std::endl;
			}

			if(clockStartL){
				timerL += m_deltaTime;
			}

			if(leftHandPosZ < leftShoulderPosZ - 0.35f && timerL < 0.1f && hitL == false){
				//std::cout << "LEFT HIT" << std::endl;
				hitL = true;
			}

			if(leftHandPosZ > leftShoulderPosZ - 0.3f && clockStartL == true ){
				//std::cout << "left back" << std::endl;
				timerL = 0.f;
				clockStartL = false;
				hitL = false;
			}
		}
	}

	// KinectEnd

	m_renderWindow->draw(*m_backgroundSprite);

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
		m_selecterSprite->move(-5.f, 0.f);
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
		m_selecterSprite->move(5.f, 0.f);
	}
	if(m_selecterSprite->getPosition().x < 780.f ){
		m_selecterSprite->setPosition(781.f, 395.f);
		m_scoreSelected = true;
	}else if(m_selecterSprite->getPosition().x > 1080.f){
		m_selecterSprite->setPosition(1079.f, 395.f);
		m_gameSelected = true;
	}else{
		m_gameSelected = false;
		m_scoreSelected = false;
	}

	if(m_selecterSprite->getPosition().x < 800.f ){
		m_renderWindow->draw(*m_scoreSelectSprite);
	}else if(m_selecterSprite->getPosition().x > 1030.f){
		m_renderWindow->draw(*m_gameSelectSprite);
	}

	if (joystickConnected == true) {
		joyX = sf::Joystick::getAxisPosition(0, sf::Joystick::X);
		joyZ = sf::Joystick::getAxisPosition(0, sf::Joystick::Z);
		//std::cout << sf::Joystick::getAxisPosition(0, sf::Joystick::X) << std::endl;
		//if(joyX>2 && Camera.getCenter().x < 930) {
		//std::cout << m_cameraX << std::endl;
		
		if(joyX>0) {
			m_selecterSprite->move(joyX * 10 * m_deltaTime, 0.f);
		}

		if(joyX<0) {
			m_selecterSprite->move(joyX * 10 * m_deltaTime, 0.f);
		}
	}

	m_renderWindow->draw(*m_selecterSprite);
}

void GameMenu::Render(){

}

void GameMenu::Cleanup(){
	delete m_backgroundTexture;
	delete m_backgroundSprite;
	delete m_gameSelectSprite;
	delete m_gameSelectTexture;
	delete m_scoreSelectSprite;
	delete m_scoreSelectTexture;
	delete m_selecterSprite;
	delete m_selecterTexture;
}